package com.example.fpe_vote.db1.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.fpe_vote.common.Search;
import com.example.fpe_vote.db1.entity.PrizeInfo;
import com.example.fpe_vote.db1.entity.VoteInfo;
import com.example.fpe_vote.db1.mapper.PrizeInfoMapper;
import com.example.fpe_vote.db1.service.INomineeInfoService;
import com.example.fpe_vote.db1.service.IPrizeInfoService;
import com.example.fpe_vote.db1.service.IVoteInfoService;
import com.example.fpe_vote.vo.request.RequestAddVoteVo;
import com.example.fpe_vote.vo.request.RequestListPrizeHtVo;
import com.example.fpe_vote.vo.request.RequestListPrizeVo;
import com.example.fpe_vote.vo.request.RequestSearchPrizeInfoVo;
import com.example.fpe_vote.vo.response.ResponseListPrizeHtVo;
import com.example.fpe_vote.vo.response.ResponseListPrizeInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.dc.pr.PRError;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2022-12-01
 */
@Service
public class PrizeInfoServiceImpl extends ServiceImpl<PrizeInfoMapper, PrizeInfo> implements IPrizeInfoService {
    @Autowired
    IVoteInfoService voteInfoService;
    @Autowired
    INomineeInfoService nomineeInfoService;

    @Override
    public IPage<ResponseListPrizeInfoVo> listPrize(RequestListPrizeVo requestListPrizeVo) {
        IPage<PrizeInfo> iPage=new Page<>(requestListPrizeVo.getDisplayStart(),requestListPrizeVo.getDisplayLength());
        QueryWrapper<PrizeInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("status","1");
        IPage<PrizeInfo> page=page(iPage,queryWrapper);
        int allTotal=list(queryWrapper).size();
        return page.convert(prizeInfo -> {
            ResponseListPrizeInfoVo vo=new ResponseListPrizeInfoVo();
            VoteInfo voteInfo=voteInfoService.queryOne(requestListPrizeVo.getUserName(),requestListPrizeVo.getGhid(),prizeInfo.getPrizeNum());
            if(ObjectUtils.isNotEmpty(voteInfo)){
                vo.setVoteStatus("1");//已投
            }else
                vo.setVoteStatus("0");//未投
            BeanUtils.copyProperties(prizeInfo,vo);
            vo.setAllTotal(allTotal);
            return vo;
        });
    }

    @Override
    public IPage<ResponseListPrizeHtVo> listPrizeHt(RequestListPrizeHtVo vo) {
        IPage<PrizeInfo> iPage=new Page<>(vo.getDisplayStart(),vo.getDisplayLength());
        QueryWrapper<PrizeInfo> queryWrapper=new QueryWrapper<>();
        if(ObjectUtils.isNotEmpty(vo.getPrizeName())){
            queryWrapper.like("prize_name",vo.getPrizeName());
        }
        if(ObjectUtils.isNotEmpty(vo.getStatus())){
            queryWrapper.eq("status",vo.getStatus());
        }
        IPage<PrizeInfo> page=page(iPage,queryWrapper);
        return page.convert(prizeInfo -> {
            ResponseListPrizeHtVo responseListPrizeHtVo=new ResponseListPrizeHtVo();
            BeanUtils.copyProperties(prizeInfo,responseListPrizeHtVo);
            int nomineeNums=nomineeInfoService.getNomineeNums(prizeInfo.getPrizeNum());
            responseListPrizeHtVo.setNomineeNums(nomineeNums);
            return responseListPrizeHtVo;
        });
    }

    @Override
    public List<ResponseListPrizeInfoVo> searchPrize(RequestSearchPrizeInfoVo vo) {
        QueryWrapper<PrizeInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("status","1");
        if(ObjectUtils.isNotEmpty(vo.getPrizeName())){
            queryWrapper.like("prize_name",vo.getPrizeName());
        }
        List<PrizeInfo> prizeInfos=list(queryWrapper);
        List<ResponseListPrizeInfoVo> voList=new ArrayList<>();
        for(PrizeInfo prizeInfo:prizeInfos){
            ResponseListPrizeInfoVo responseListPrizeInfoVo=new ResponseListPrizeInfoVo();
            BeanUtils.copyProperties(prizeInfo,responseListPrizeInfoVo);
            voList.add(responseListPrizeInfoVo);
        }
        return voList;
    }

    @Override
    public PrizeInfo queryByNum(String prizeNum) {
        QueryWrapper<PrizeInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("prize_num", prizeNum);
        List<PrizeInfo> list=list(queryWrapper);
        if(list==null||list.size()==0){
            return null;
        }else
            return getOne(queryWrapper);
    }
}
